<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            border: dashed 2px pink;
            width: 600px;
            height: 400px;
            margin: auto;
            text-align: center;
        }
        li{
            list-style-type: none;
            display: inline-block;
            width: 100px;
            height: 100px;
            line-height: 100px;
            margin: 0 16px;
            font-size: 20px;
            font-weight: 700;
            color: blue;
            background: pink;
            border-radius: 50%;
            
        }
    </style>
</head>
<body>
        <div class="box">
            <h1>双色球</h1>
            <ul>
                <li>00</li>
                <li>00</li>
                <li>00</li>
                <li>00</li>
            </ul>
            <button>开始</button>
            <button>结束</button>
        </div>
        <script>
            var oli = document.getElementsByTagName('li')
            var btn = document.getElementsByTagName('button')
            var i = 0
            var n = 0
            var time = null
          
            btn[0].onclick = function(){
                // 点击之前先清除定时器
                clearInterval(time)
                // 点击直接开始将随机数值给到 oli的innerHTML
                function fn(){
                    // 通过遍历将数值给到每个li
                    // 这里如果 n = 1 那么 i=1 就不会去遍历1前面的数据,从而实现停止的效果
                    for(i = n;i<oli.length;i++) {
                        oli[i].innerHTML = (Math.floor((Math.random()) * (30+1) -1))
                    }
                }
                time = setInterval(fn,50)
            }
            btn[1].onclick = function(){
                n++
                if(n>oli.length) {
                    clearInterval(time)
                    n=0
                }
            }
        </script>
</body>
</html>